<?php

if (version_compare(phpversion(), '5.1.0', '<') == true) {
	die ('PHP5.1 Only');
}

session_name("uzdaewoo_centr");
session_start();

mb_internal_encoding('UTF-8');

define('DIRSEP',   DIRECTORY_SEPARATOR);
define('ROOT_DIR', realpath(dirname(__FILE__).DIRSEP.'..'.DIRSEP).DIRSEP);
define('TEMPLATE_SITE',  ROOT_DIR.'templates/site/');
define('TEMPLATE_ADMIN', ROOT_DIR.'templates/admin/');
define('PRFX',     '');
define('DB_STRUCTURE', PRFX.'structure');
define('ENDURL', '.html');

/* Автозагрузка Классов */
function autoload($class_name)
{	
	if(!class_exists($class_name, false))
	{		
		$Path_Class      = ROOT_DIR."classes".DIRSEP.$class_name.'.class.php';
		$Path_Controller = ROOT_DIR."controller".DIRSEP.$class_name.'.php';		
		
		if(function_exists('smartyAutoload') && smartyAutoload($class_name)) {
			return true;
		}
		elseif(file_exists($Path_Class)) {
			require_once($Path_Class);
		}
		elseif(file_exists($Path_Controller)) {
			require_once($Path_Controller);
		}
		else
		{
			$coreClass = new ReflectionClass($class_name.'Core');
			if ($coreClass->isAbstract()) {
				eval('abstract class '.$class_name.' extends '.$class_name.'Core {}');
			}
			else {
				eval('class '.$class_name.' extends '.$class_name.'Core {}');
			}
		}
	}
	else {
		return false;
	}	
}

require_once('Registry.php');
require_once(ROOT_DIR.'/core/pear/PEAR.php');
require_once('smarty/Smarty.class.php');

spl_autoload_register("autoload");

/************
Smarty Config
************/
$smarty = new Smarty;
$smarty->caching        = false;
$smarty->cache_lifetime = 120;
$smarty->compile_dir    = ROOT_DIR.'templates_c/';
$smarty->config_dir     = ROOT_DIR.'configs/';
$smarty->cache_dir      = ROOT_DIR.'cache/';
$smarty->debugging      = false;
$smarty->allow_php_tag  = true;

Registry::set("smarty",   $smarty);
Registry::set("cache_id", md5($_SERVER['REQUEST_URI']));




/*************
MYSQL Config
*************/
require_once(ROOT_DIR.'/core/pear/DB/MDB2.php');
require_once(ROOT_DIR.'/core/pear/DB/Driver/mysql.php');
$dsn = parse_ini_file($smarty->config_dir."db.ini", 1);
$options = array(
	'debug'       => 2,
	'portability' => MDB2_PORTABILITY_ALL
);
$mdb2 =& MDB2::connect($dsn["database"], $options);
if (PEAR::isError($mdb2)) {
	die($mdb2->getMessage().' ::: '.$mdb2->getUserInfo());
}
$mdb2->query("SET NAMES `utf8`;");
$mdb2->query("SET CHARACTER SET `utf8`;");
setlocale(LC_ALL, "ru_RU.UTF-8");

Registry::set("DB_NAME", $dsn["database"]["database"]);
/*************
END MYSQL Config
*************/

/* Set Constant's */
$r =& $mdb2->query('SELECT `Const`, `Value` FROM `'.PRFX.'config`;');
if (PEAR::isError($r)) {
	die($r->getMessage().' ::: '.$r->getUserInfo());
}
while ($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC)) {
	define($p["const"], $p["value"]);
}

Registry::set("MDB", $mdb2);

?>